package com.coocaa.x.app.hall.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.view.View;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;

/**
 * Created by yibiaoshi on 2015/7/8.
 */
public class ReflectionView extends View {
    private Bitmap roundBitmap;
    private Bitmap reflectBitmap;
    private Bitmap bitmap;
    private Canvas roundCanvas;
    private int mWidth = 0;
    private int mHeight = 0;

    public ReflectionView(Context context, int width, int height) {
        super(context);
        this.mWidth = width;
        this.mHeight = height;
    }

    public void setReflectionBitmap(Bitmap bitmap) {
        if (bitmap == null)
            return;
        this.bitmap = bitmap;
        invalidate();
    }

    public void onDestroy() {
        if (roundCanvas != null) {
            roundCanvas = null;
        }

        if (roundBitmap != null && !roundBitmap.isRecycled()) {
            roundBitmap.recycle();
            roundBitmap = null;
        }

        if (reflectBitmap != null && !reflectBitmap.isRecycled()) {
            reflectBitmap.recycle();
            reflectBitmap = null;
        }

        if (bitmap != null && !bitmap.isRecycled())
            bitmap.recycle();

        destroyDrawingCache();
    }

    private Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

        if (bitmap == null)
            return null;

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        roundCanvas = new Canvas(output);
        roundCanvas.setBitmap(output);
        int color = 0xff424242;
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0,
                bitmap.getWidth(), bitmap.getHeight());
        RectF rectF = new RectF(rect);
        float roundPx = 12;
        paint.setAntiAlias(true);

        roundCanvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        roundCanvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        roundCanvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    /**
     * 获得带倒影的图片方法
     *
     * @param originalImage
     * @return
     */
    public Bitmap createReflectionImageWithOrigin(Bitmap originalImage) {
        if (originalImage == null)
            return null;

        int width = originalImage.getWidth();
        int height = originalImage.getHeight();

        Matrix matrix = new Matrix();
        if (mWidth > 0 && mHeight > 0)
            matrix.postScale((float) mWidth / width, (float) mHeight / height);
        // 图片矩阵变换（从低部向顶部的倒影）// 实现图片翻转90度
        matrix.preScale(1, -1);
        // 创建反转后的图片Bitmap对象
        Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height - CoocaaApplication.Div(100),
                width, CoocaaApplication.Div(100), matrix, false);
        // 创建画布对象，将原图画于画布，起点是原点位置
        roundCanvas = new Canvas(reflectionImage);
        // 将反转后的图片画到画布中
        Paint paint = new Paint();
        // 创建线性渐变LinearGradient对象
        LinearGradient shader = new LinearGradient(0, 0, 0,
                reflectionImage.getHeight(), 0x30ffffff,
                0x00ffffff, Shader.TileMode.CLAMP);

        paint.setAlpha(0x99);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        // 画布画出反转图片大小区域，然后把渐变效果加到其中，就出现了图片的倒影效果
        roundCanvas.drawRect(0, 0, reflectionImage.getWidth(), reflectionImage.getHeight(), paint);
        if (!originalImage.isRecycled()) {
            originalImage.recycle();
        }

        return reflectionImage;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (bitmap != null) {
            try {
                if (reflectBitmap == null || reflectBitmap.isRecycled()) {
                    roundBitmap = getRoundedCornerBitmap(bitmap);
                    reflectBitmap = createReflectionImageWithOrigin(roundBitmap);
                }

                if (reflectBitmap != null)
                    canvas.drawBitmap(reflectBitmap, 0, 0, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
